using System;
using System.Collections.Generic;
using System.Text;

using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

namespace addCollate
{
    class addCollate
    {
        static void Main(string[] args)
        {
            int c = Environment.TickCount;

            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);
            sqlparser.SqlText.Text = @"select [IMINVLOC_SQL].[QTY_ON_ORD], 

[IMINVLOC_SQL].[AVG_COST], 

[IMINVLOC_SQL].[PRICE], 

[IMINVLOC_SQL].[A4GLIDENTITY], 

[IMINVLOC_SQL].[STATUS] collate SQL_Latin1_General_CP1_CI_AS,        

[IMINVLOC_SQL].[LOC], [IMINVLOC_SQL].[ITEM_NO],

[IMINVLOC_SQL].[LOC], [IMINVLOC_SQL].[ITEM_DESC],  

(select IMITMIDX_SQL.prod_cat collate SQL_Latin1_General_CP1_CI_AS 

from [machine-001].[demodata].dbo.IMITMIDX_SQL 

where item_no=IMINVLOC_SQL.item_no) as prodcat,    

FROM [machine-001].[demodata].[DBO].[IMINVLOC_SQL]";

            int iRet = sqlparser.Parse();

            if (iRet == 0){
                TSelectSqlStatement select = sqlparser.SqlStatements[0] as TSelectSqlStatement;
                foreach (TLzField fld in select.Fields)
                {
                    if (fld.FieldName.CompareTo("[ITEM_DESC]") == 0)
                    {
                        fld.AsText = fld.AsText + " collate SQL_Latin1_General_CP1_CI_AS";
                    }
                }

                Console.WriteLine(select.AsText);
                //Console.WriteLine("Input sql parsed ok!");
            }else{
               Console.WriteLine(sqlparser.ErrorMessages);
            }
                            
             Console.WriteLine("Time escaped after parsing: {0}", Environment.TickCount - c);
                          

        }
    }
}
